use std::cell::RefCell;
use std::thread;
use std::thread::current;

// 模拟测试多线程，以及tls
fn main() {
    // 主线程定义只属于主线程自己的静态变量 FOO
    thread_local! { static FOO : RefCell<u32> = RefCell::new(1)};

    FOO.with(|f| {
        println!("主线程的f: {:p}", f);
        assert_eq!(*f.borrow(), 1);
        *f.borrow_mut() = 2;
    });

    let handle = thread::spawn(move || {
        FOO.with(|f| {
            println!("子线程的f: {:p}", f);
            assert_eq!(*f.borrow(), 1);
            *f.borrow_mut() = 3;
        });
    });

    handle.join().unwrap();

    FOO.with(|f| {
        assert_eq!(*f.borrow(), 2);
    });
}
